/**
 * @file 菜单列表配置
 * @author liushuang(liushuang04@baijia.com), andycao(caohuayu@baijia.com)
 * @update_date 2019-11-23
 */
import {PAGE_ENUM} from '~/utils/menu';

/**
 * 遍历树结构，查找目标节点，并执行处理函数
 *
 * @param {Array} nodes 树结构数据
 * @param {string|null} targetId 目标节点ID，若 targetId 传 null，表示对每个节点都进行操作
 * @param {Function} handler 对目标节点的处理函数
 */

export const loopLeafs = function (nodes, targetId, handler) {
    for (let index = 0; index < nodes.length; index++) {
        const node = nodes[index];
        const {id, children} = node;

        if (targetId && targetId.toString() === id.toString()) {
            handler(node, nodes, index);
            break;
        }
        // 若 targetId 传 null，表示对每个节点都进行操作
        else if (!targetId) {
            handler(node, nodes, index);
        }

        if (children) {
            loopLeafs(children, targetId, handler);
        }
    }
};

/**
 * 菜单数据处理
 *
 * @param {data} array 数据
 * @return {options} {返回配置}
 */
export const formatMenu = list => {
    loopLeafs(list, null, node => {
        if (node.type === 2) {
            mapRoutes(node);
        }
        if (node.type === 1) {
            node.key = `report_sub_${node.id}`;
        }
    });
    return list;
};

/**
 * 查询menu list 中的所有父节点，及文件夹
 * @param {treeData} treeData menu数据
 * @param {node} target 截止点，null时全部遍历
 */
export const getParentKeys = (treeData, target) => {
    const parentKey = [];
    loopLeafs(treeData, target, n => {
        if (n.children) {
            parentKey.push(n.key.toString());
        }
    });
    return parentKey;
};

/**
 * 根据id parentid转变为页面路由, 设置path key
 * 在原对象中添加
 * @param {Object<{id, parentId}>} element 页面信息
 */
function mapRoutes(element) {
    switch (element.id) {
        case PAGE_ENUM.PAGES_PERFORMANCE_ANALYSIS:
            element.path = `/perform/analysis/${element.id}`;
            element.key = `perform_analysis_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.CORE_PAGES_OVERVIEW:
            element.path = `/perform/corePage/${element.id}`;
            element.key = `perform_corePage_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.PAGES_PERFORMANCE_RANK:
            element.path = `/perform/rank/${element.id}`;
            element.key = `perform_rank_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.USER_FEATURE:
            element.path = `/perform/userFeature/${element.id}`;
            element.key = `perform_userFeature_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.GAO_TU_CORE_PAGES_MONITOR:
            element.path = `/perform/gaotuCore/${element.id}`;
            element.key = `perform_gaotuCore_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.SELF_CONFIG:
            element.path = `/manage/selfConfig/${element.id}`;
            element.key = `manage_selfConfig_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.BIG_SCREEN_CONFIG:
            element.path = `/manage/screen/list/${element.id}`;
            element.key = `manage_screen_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.HAOKE_SALES_REPORT:
            element.path = `/sales/haoke/${element.id}`;
            element.key = `sales_haoke_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.GAO_TU_XU_BAN_SECOND:
        case PAGE_ENUM.GAO_TU_XU_BAN_FIRST:
            element.path = `/renew/board/${element.id}`;
            element.key = `renew_board_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.HAO_KE_XING_KE_REPORT:
        case PAGE_ENUM.GAO_TU_XING_KE_REPORT:
            element.path = `/report/xingke/${element.id}`;
            element.key = `report_xingke_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.liveRoom:
            element.path = '/liveMonitor/list';
            element.key = `liveMonitor_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.liveRoomDetail:
            element.path = `/liveMonitor/detail/${element.id}`;
            element.key = `liveMonitor_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.OVERALL_TREND:
            element.path = '/liveMonitor/overallTrend';
            element.key = `liveMonitor_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.WEBAPI:
            element.path = '/liveMonitor/webapi';
            element.key = `liveMonitor_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.ROOMSERVER:
            element.path = '/liveMonitor/roomserver';
            element.key = `liveMonitor_${element.parentId}_${element.id}`;
            break;
        case PAGE_ENUM.STREAMING_MEDIA:
            element.path = '/liveMonitor/streamingMedia';
            element.key = `liveMonitor_${element.parentId}_${element.id}`;
            break;
        default:
            element.path = `/report/info/${element.parentId}/${element.id}`;
            element.key = `report_info_${element.parentId}_${element.id}`;
    }
}

let parentNode = null;
let node = null;
const result = [];
const getNode = (json, key) => {
    for (let i = 0; i < json.length; i++) {
        if (node) {
            break;
        }
        const obj = json[i];
        if (!obj || !obj.key) {
            continue;
        }
        if (obj.key === key) {
            node = obj;
            break;
        }
        else if (obj.children && obj.children.length) {
            parentNode = obj;
            getNode(obj.children, key);
        }
        else {
            continue;
        }
    }

    if (!node) {
        parentNode = null;
    }

    return {
        parentNode,
        node
    };
};

export const getResult = (json, query) => {
    node = null;
    parentNode = null;
    const obj = getNode(json, query);
    if (obj.parentNode) {
        getResult(json, obj.parentNode.key);
        result.push(obj.parentNode.key);
    }
    return result;
};

export const parentAll = (params, nodes) => {
    const {id, parentId, key, type} = params;
    let temp = [];
    const forFn = function (key, type) {
        const nodesFilter = nodes.filter(
            item => item.key === key && item.type === type
        );
        temp.push(nodesFilter[0].key);
        const parentNodes = nodes.filter(
            item => item.id === nodesFilter[0].parentId && item.type === 1
        );
        if (parentNodes.length) {
            forFn(parentNodes[0].key, parentNodes[0].type);
        }
        return temp;
    };
    temp = forFn(key, type);
    return temp;
};
